<?php
header('content-type:text/html;charset=utf-8' );
class DbCon() {
	
	private $db_host = "localhost";
	private $db_user = "root";
	private $db_pass = "root";
	private $db_name = "test";
	private $db_port = 3306;
	
	function __construct() {
	$link = mysqli_connect($db_host, $db_user, $db_pass, $db_name, $db_port);
	if ($link) {
		return $link;
	}
	else {
		die('无法连接数据库' . mysqli_error($link));
	}
	}

	function query($link, $sql)
	{
		if($result = mysqli_query($link, $sql))
		{
			return $result;
		}
		else
		{
			echo '错误SQL语句为：',$sql,'<br>';
			echo '错误代码为：',mysqli_errno($link),'<br>';
			echo '错误信息为：',mysqli_error($link),'<br>';
			die;
		}
	}

	//处理多条数据
	function fetchAll($link, $sql)
	{
		if($result = query($link, $sql))
		{
			$rows = array();
			while($row = mysqli_fetch_array($result, MYSQL_ASSOC))
			{
				$rows[] = $row;
			}
			mysqli_free_result($result);
			return $rows;
		}
		else
		{
			return false;
		}
	}

	//处理单挑数据
	function fetchRow($link, $sql)
	{
		if($result = query($link,$sql))
		{
			$row = mysqli_fetch_array($result, MYSQL_ASSOC);
			return $row;
		}
		else
		{
			return false;
		}
	}

	//数据安全处理
	function safeHandle($link, $data)
	{
		$data = htmlspecialchars($data);
		$data = mysqli_real_escape_string($link, $data);
		return $data;
	}
}
?>